package net.suncaper.hotel.mapper.impl;

import net.suncaper.hotel.common.utils.JDBCUtils;
import net.suncaper.hotel.mapper.IRoomTypeMapper;
import net.suncaper.hotel.model.domain.RoomType;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 房间类型数据访问实现类
 */
public class RoomTypeMapperImpl implements IRoomTypeMapper {
    private QueryRunner queryRunner = new QueryRunner();

    @Override
    public RoomType findById(Integer id) {
        String sql = "SELECT * FROM room_type WHERE id = ?";
        try {
            return queryRunner.query(JDBCUtils.getConnection(), sql,
                new BeanHandler<>(RoomType.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<RoomType> findAll() {
        String sql = "SELECT * FROM room_type ORDER BY create_time DESC";
        try {
            return queryRunner.query(JDBCUtils.getConnection(), sql,
                new BeanListHandler<>(RoomType.class));
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<RoomType> findAvailable() {
        String sql = "SELECT * FROM room_type WHERE status = 1 ORDER BY price";
        try {
            return queryRunner.query(JDBCUtils.getConnection(), sql,
                new BeanListHandler<>(RoomType.class));
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int insert(RoomType roomType) {
        String sql = "INSERT INTO room_type (type_name, bed_type, max_occupancy, room_size, " +
                     "price, description, amenities, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        try {
            return queryRunner.update(JDBCUtils.getConnection(), sql,
                roomType.getTypeName(), roomType.getBedType(), roomType.getMaxOccupancy(),
                roomType.getRoomSize(), roomType.getPrice(), roomType.getDescription(),
                roomType.getAmenities(), roomType.getStatus());
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int update(RoomType roomType) {
        String sql = "UPDATE room_type SET type_name = ?, bed_type = ?, max_occupancy = ?, " +
                     "room_size = ?, price = ?, description = ?, amenities = ?, status = ? WHERE id = ?";
        try {
            return queryRunner.update(JDBCUtils.getConnection(), sql,
                roomType.getTypeName(), roomType.getBedType(), roomType.getMaxOccupancy(),
                roomType.getRoomSize(), roomType.getPrice(), roomType.getDescription(),
                roomType.getAmenities(), roomType.getStatus(), roomType.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int delete(Integer id) {
        String sql = "DELETE FROM room_type WHERE id = ?";
        try {
            return queryRunner.update(JDBCUtils.getConnection(), sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}